package org.duanyu.shopping_goods_service.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.duanyu.shopping_common.pojo.*;
import org.duanyu.shopping_common.service.GoodsService;
import org.duanyu.shopping_goods_service.mapper.GoodsImageMapper;
import org.duanyu.shopping_goods_service.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@DubboService
@Transactional
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsImageMapper goodsImageMapper;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    //同步商品数据主题
    private final String SYNC_GOOD_QUEUE = "sync_goods_queue";
    //删除商品数据主题
    private final String DEL_GOOD_QUEUE = "del_goods_queue";
    //同步商品到购物车的主题
    private final String SYNC_CART_QUEUE = "sync_cart_queue";
    //删除商品到购物车的主题
    private final String DEL_CART_QUEUE = "del_cart_queue";

    @Override
    public void add(Goods goods) {
        //插入商品数据
        goodsMapper.insert(goods);
        //插入图片数据
        //获取商品主键
        Long goodsId = goods.getId();
        List<GoodsImage> images = goods.getImages();
        for (GoodsImage image : images) {
            image.setGoodsId(goodsId); //给图片设置商品id
            goodsImageMapper.insert(image);
        }

        //插入商品_规格项数据
        //1.获取规格
        List<Specification> specifications = goods.getSpecifications();
        //2.获取规格项
        List<SpecificationOption> options = new ArrayList<>();
        //便利规格，获取规格中的所有规格项
        for (Specification specification : specifications) {
            options.addAll(specification.getSpecificationOptions());
        }
        //遍历规格项，插入商品规格项数据
        for (SpecificationOption option : options) {
            goodsMapper.addGoodsSpecificationOption(goodsId, option.getId());
        }

        //将商品数据同步到ES中
        GoodsDesc desc = findDesc(goodsId);
        rocketMQTemplate.syncSend(SYNC_GOOD_QUEUE, desc);


    }

    @Override
    public void update(Goods goods) {
        //删除旧图片数据
        Long goodsId = goods.getId(); //获取商品主键
        QueryWrapper<GoodsImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("goodsId",goodsId);
        goodsImageMapper.delete(queryWrapper);
        //删除旧规格数据
        goodsMapper.deleteGoodsSpecificationOption(goodsId);

        //修改商品数据
        goodsMapper.updateById(goods);
        //插入图片数据
        List<GoodsImage> images = goods.getImages();
        for (GoodsImage image : images) {
            image.setGoodsId(goodsId); //给图片设置商品id
            goodsImageMapper.insert(image);
        }
        //插入商品_规格项数据
        //1.获取规格
        List<Specification> specifications = goods.getSpecifications();
        //2.获取规格项
        List<SpecificationOption> options = new ArrayList<>();
        //便利规格，获取规格中的所有规格项
        for (Specification specification : specifications) {
            options.addAll(specification.getSpecificationOptions());
        }
        //遍历规格项，插入商品规格项数据
        for (SpecificationOption option : options) {
            goodsMapper.addGoodsSpecificationOption(goodsId, option.getId());
        }

        //将商品数据同步到ES中
        GoodsDesc desc = findDesc(goodsId);
        rocketMQTemplate.syncSend(SYNC_GOOD_QUEUE, desc);

        //将商品数据同步到购物车中
        CartGoods cartGoods = new CartGoods();
        cartGoods.setGoodId(goods.getId());
        cartGoods.setGoodsName(goods.getGoodsName());
        cartGoods.setHeaderPic(goods.getHeaderPic());
        cartGoods.setPrice(goods.getPrice());
        rocketMQTemplate.syncSend(SYNC_CART_QUEUE, cartGoods);
    }

    @Override
    public Goods findById(Long id) {
        return goodsMapper.findById(id);
    }

    @Override
    public void putAway(Long id, Boolean isMarketable) {
        goodsMapper.putAway(id, isMarketable);
        //上架时数据同步到es，下架时删除es数据
        if (isMarketable) {
            //将商品数据同步到ES中
            GoodsDesc goodsDesc = findDesc(id);
            rocketMQTemplate.syncSend(SYNC_GOOD_QUEUE, goodsDesc);
        } else {
            rocketMQTemplate.syncSend(DEL_GOOD_QUEUE, id);
            //将购物车中的商品删除
            rocketMQTemplate.syncSend(DEL_CART_QUEUE, id);

        }
    }

    @Override
    public Page<Goods> search(Goods goods, int page, int size) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        //判断商品名是否为空
        if (goods != null && StringUtils.hasText(goods.getGoodsName())) {
            wrapper.like("goodsName", goods.getGoodsName());
        }
        return goodsMapper.selectPage(new Page<>(page, size), wrapper);
    }

    @Override
    public List<GoodsDesc> findAll() {
        return goodsMapper.findAll();
    }

    @Override
    public GoodsDesc findDesc(Long id) {
        return goodsMapper.findDesc(id);
    }
}
